package com.weixx.yctp.cp.download;

import com.weixx.yctp.cp.entity.Cp;
import org.apache.poi.EmptyFileException;
import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

public class DownUtil {

    public void excelDownloadUtile(List<Map<String,Object>> entityMapList,HttpServletResponse response,boolean flag) throws IOException {

        List<String> header=new ArrayList<>();
        if (entityMapList.size()<=0){
            throw new RuntimeException("此区间没有数据");
        }
        //1.根据entityMapList 获取表头
        Set<String> headerS = entityMapList.get(0).keySet();
        //2.根据根据entityMapList 获取List<List<string>>
        List<String> chineseName = getChineseMap(headerS);

        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();

        //生成一个表格，设置表格名称为"学生表"
        HSSFSheet sheet = workbook.createSheet("测评记录");

        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(10);

        //创建第一行表头
        HSSFRow headrow = sheet.createRow(0);

        int count=0;
        //遍历添加表头
        List<String> nameMap=new ArrayList<>();
        for (String s : headerS) {
            nameMap.add(s);
        }

        for (String s : chineseName) {
            //创建一个单元格
            HSSFCell cell = headrow.createCell(count);
            count++;
            //创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(s);

            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if(flag) {
            for (Map<String, Object> stringObjectMap : entityMapList) {
                if(stringObjectMap.get("createTime")!=null&&stringObjectMap.get("createTime")!=null) {
                    String createTime = sdf.format(new Date(Long.valueOf(stringObjectMap.get("createTime").toString())));
                    String updateTime = sdf.format(new Date(Long.valueOf(stringObjectMap.get("updateTime").toString())));
                    stringObjectMap.put("createTime", createTime);
                    stringObjectMap.put("updateTime", updateTime);
                }
            }
        }


        for (int j = 0; j < entityMapList.size(); j++) {
            HSSFRow rows=sheet.createRow(j+1);
            for (int i = 0; i < entityMapList.get(0).size(); i++) {
                HSSFCell cell = rows.createCell(i);
                HSSFRichTextString text=null;
                if (entityMapList.get(j).get(nameMap.get(i))!=null){
                     text = new HSSFRichTextString(entityMapList.get(j).get(nameMap.get(i)).toString());
                }
                else{
                     text = new HSSFRichTextString("/");
                }

                cell.setCellValue(text);
            }
        }



        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
       // response.setContentType("application/octet-stream");
       response.setContentType("application/vnd.ms-excel");

        //这后面可以设置导出Excel的名称，此例中名为student.xls
     //   response.setHeader("Content-disposition", "attachment;filename=cpData.xlsx");

        response.setHeader("Content-Disposition", "attachment;fileName=" +
                URLEncoder.encode("cpData.xlsx","utf8"));
//
        //
       // response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");

        //刷新缓冲
        response.flushBuffer();

        //workbook将Excel写入到response的输出流中，供页面下载
        workbook.write(response.getOutputStream());
    }
    public List<String> getChineseMap(Set<String> headerS){
        Map<String, String> yXmap = getYXmap();
        List<String> newList=new ArrayList<>();
        for (String header : headerS) {
            if (yXmap.get(header)!=null){
                newList.add(yXmap.get(header));
            }
            else{
                newList.add(header);
            }
        }
        return newList;

    }

    //字符串和中文对照
    public Map<String,String> getYXmap(){
        Map<String,String> changeName=new HashMap<>();
        changeName.put("sfgw","1.市发改委");
        changeName.put("sfgw_BZ","市发改委意见");
        changeName.put("sjxj","2.市区经信局");
        changeName.put("sjxj_BZ","市区经信局意见");
        changeName.put("sczj","3.市财政局");
        changeName.put("sczj_BZ","市财政局意见");
        changeName.put("sscjgj","4.市市场监管局");
        changeName.put("sscjgj_BZ","市市场监管局意见");
        changeName.put("szrzyhghj","5.市自然资源和规划局");
        changeName.put("szrzyhghj_BZ","市自然资源和规划局意见");
        changeName.put("slyhylj","6.市林业和园林局");
        changeName.put("slyhylj_BZ","市林业和园林局意见");
        changeName.put("snyncj","7.市农业农村局");
        changeName.put("snyncj_BZ","市农业农村局意见");
        changeName.put("szwfwhdsjglj","8.市政务服务和大数据管理局");
        changeName.put("szwfwhdsjglj_BZ","市政务服务和大数据管理局意见");
        changeName.put("ssthjj","9.市生态环境局");
        changeName.put("ssthjj_BZ","市生态环境局意见");
        changeName.put("syjglj","10.市应急管理局");
        changeName.put("syjglj_BZ","市应急管理局意见");
        changeName.put("sslhpj","11.市水利湖泊局");
        changeName.put("sslhpj_BZ","市水利湖泊局意见");
        changeName.put("swhhlyj","12.市文化和旅游局");
        changeName.put("swhhlyj_BZ","市文化和旅游局意见");
        changeName.put("szjj","13.市住建局");
        changeName.put("szjj_BZ","市住建局意见");
        changeName.put("sgdgs","14.市供电公司");
        changeName.put("sgdgs_BZ","市供电公司意见");
        changeName.put("sxfjyzd","15.市消防救援支队");
        changeName.put("sxfjyzd_BZ","市消防救援支队意见");
        changeName.put("srfb","16.市消防救援支队");
        changeName.put("srfb_BZ","市消防救援支队意见");
        changeName.put("sqxj","17.市气象局");
        changeName.put("sqxj_BZ","市气象局意见");
        changeName.put("ycsxswyxgs","18.宜昌三峡水务有限公司");
        changeName.put("ycsxswyxgs_BZ","宜昌三峡水务有限公司意见");
        changeName.put("yczrcsrqyxgs","19.宜昌中燃城市燃气有限公司");
        changeName.put("yczrcsrqyxgs_BZ","宜昌中燃城市燃气有限公司意见");

        return changeName;


    }




}
